package com.woniu.service.medicalService.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.mapper.medicalService.DepartmentMapper;
import com.woniu.model.medicalService.entity.Department;
import com.woniu.model.gh.common.enums.RedisConstant;
import com.woniu.model.gh.utils.PageVo;
import com.woniu.model.medicalService.vo.DepartmentVo;
import com.woniu.service.medicalService.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

//科室表
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ElasticsearchRestTemplate esRestTemplate;

    //初始化科室信息
    @Override
    public void initDepartment() {

        if (redisTemplate.hasKey(RedisConstant.I_DEPARTMENT_LIST))
            redisTemplate.delete(RedisConstant.I_DEPARTMENT_LIST);

        if (redisTemplate.hasKey(RedisConstant.II_DEPARTMENT_LIST))
            redisTemplate.delete(RedisConstant.II_DEPARTMENT_LIST);

        List<Department> iList = list(Wrappers.lambdaQuery(Department.class).eq(Department::getPId, 0));
        List<Department> iiList = list(Wrappers.lambdaQuery(Department.class).ne(Department::getPId, 0));

        redisTemplate.opsForList().rightPushAll(RedisConstant.I_DEPARTMENT_LIST, iList);
        redisTemplate.opsForList().rightPushAll(RedisConstant.II_DEPARTMENT_LIST, iiList);
    }

    //分页 条件查询(根据二级科室名称模糊查询)
    @Override
    public PageVo<DepartmentVo> findPageByKeyword(Long firstLevelId, String secondLevelName, Integer pageNum, Integer pageSize) {
        List<Department> firstLevelList = getDepartmentList(redisTemplate.opsForList().range(RedisConstant.I_DEPARTMENT_LIST,0,-1));
        List<Department> secondLevelList = baseMapper.findPageByKeyword(firstLevelId,secondLevelName);
        ArrayList<DepartmentVo> list = new ArrayList<>();
        firstLevelList.forEach(f->{
            secondLevelList.forEach(s->{
                if (f.getId().equals(s.getPId())){
                    DepartmentVo vo = new DepartmentVo(f.getId(),f.getName(),s.getId(),s.getName());
                    list.add(vo);
                }
            });
        });
        return new PageVo<>(pageNum, pageSize, list);
    }

    //查询所有一级科室
    @Override
    public List<Department> getFirstLevels() {
        List<Map<String, Object>> list =
                redisTemplate.opsForList().range(RedisConstant.I_DEPARTMENT_LIST, 0, -1);

        assert list != null;
        return getDepartmentList(list);
    }

    //查询所有二级科室
    @Override
    public List<Department> getSecondLevels() {
        List<Map<String, Object>> list =
                redisTemplate.opsForList().range(RedisConstant.II_DEPARTMENT_LIST, 0, -1);

        assert list != null;
        return getDepartmentList(list);
    }

    //查询所有科室列表
    @Override
    public List<Department> list() {
        List<Map<String, Object>> reIList =
                redisTemplate.opsForList().range(RedisConstant.I_DEPARTMENT_LIST, 0, -1);

        List<Map<String, Object>> reIIList =
                redisTemplate.opsForList().range(RedisConstant.II_DEPARTMENT_LIST, 0, -1);

        assert reIList != null;
        List<Map<String, Object>> reList = new ArrayList<>(reIList);

        assert reIIList != null;
        reList.addAll(reIIList);

        return getDepartmentList(reList);
    }

    //获取科室对象集合
    private List<Department> getDepartmentList(List<Map<String, Object>> redisList) {
        List<Department> list = new ArrayList<>();
        redisList.forEach(n -> {
            Department de = new Department();
            n.forEach((key, value) -> {
                if (key.equals("id")) de.setId(Long.valueOf(value.toString()));
                if (key.equals("grade")) de.setGrade(Integer.valueOf(value.toString()));
                if (key.equals("name")) de.setName(value.toString());
                if (key.equals("pId")) de.setPId(Long.valueOf(value.toString()));
            });
            list.add(de);
        });
        return list;
    }

}
